<template>
  <div class="import">
    <el-card>
      <h3 class="top">{{ name }}导入</h3>
      <ReadExcel :before-upload="beforeUpload" :on-success="onSuccess" />
    </el-card>
  </div>
</template>

<script>
import { sysUserBatch } from '@/api/employess.js'
export default {
  data() {
    return { name: this.$route.query.name }
  },
  methods: {
    beforeUpload(file) {
      console.log(file)
      return true
    },
    async onSuccess({ header, results }) {
      const obj = {
        转正日期: 'correctionTime',
        手机号: 'mobile',
        入职日期: 'timeOfEntry',
        姓名: 'username',
        工号: 'workNumber'
      }
      const newArr = results.map((item) => {
        const temp = {}
        Object.keys(item).forEach((item2) => {
          temp[obj[item2]] = item[item2]
          if (obj[item2] === 'correctionTime' || obj[item2] === 'timeOfEntry') {
            const t1 = new Date(
              (item[item2] - 1) * 24 * 60 * 60 * 1000 - 8 * 60 * 60 * 1000
            )
            t1.setYear(t1.getFullYear() - 70)
            temp[obj[item2]] = t1
          }
        })
        return temp
      })

      await sysUserBatch(newArr)
      this.$message.success('导入成功')
      this.$router.push(this.$route.query.redirect || '/employees')
      console.log(newArr)
    }
  }
}
</script>

<style lang="scss" scoped>
.top {
  text-align: center;
}
</style>
